<template>
  <div>
    <el-dialog
      :title="`${!isEdit?'添加':'编辑'}分类`"
      :visible="dialogVisible"
      @close="$emit('watchChild')"
      @open="openPage">
      <el-form :model="cateform" class="dialog_form">
        <el-form-item :label="'名称'" :label-width="formLabelWidth">
          <el-input v-model="cateform.name"></el-input>
        </el-form-item>
        <el-form-item :label="'排序'" :label-width="formLabelWidth">
          <el-input v-model="cateform.sort"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('watchChild')">取 消</el-button>
        <el-button type="primary" @click="onConfirms">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { CateAdd, saveOrderUpdate } from '@/services/resource'
import Vue from 'vue'

export default Vue.extend({
  name: 'AddCate',
  props: {
    isEdit: {
      type: Boolean
    },
    dialogVisible: {
      type: Boolean
    },
    cate_name: {
      type: String
    },
    cate_sort: {
      type: Number
    },
    cate_id: {
      type: Number
    }
    // currentData: {
    // type: Object
    // default: {}
    // }
  },
  data () {
    return {
      // dialogFormVisible: this.dialogVisible,
      cateform: {
        name: '',
        sort: 0
      },
      formLabelWidth: '70px'
      // findId: 0
    }
  },
  methods: {
    // recordResourceBol (val:boolean, id?:number) {
    //   this.dialogFormVisible = val// this.dialogVisible

    //   if (id) {
    //     this.findId = id
    //   }
    // },
    async onConfirms () {
      this.$confirm(`此操作将${!this.isEdit ? '添加' : '编辑'}一项分类, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // console.log('此时数据', this.cateform)

        const { data } = await saveOrderUpdate(this.cateform)
        // console.log('添加分类', data)
        if (data.code === '000000') {
          this.$message.success(`${!this.isEdit ? '添加' : '编辑'}成功`)
          // this.dialogFormVisible = false
          this.$emit('watchChild')
        }
      }).catch(() => {
        this.$message.error('添加失败，请重试')
      })
    },
    // async handlerEdit () {
    //   const { data } = await getAllCategory(this.id)
    //   console.log('dataAA', data)
    // }
    openPage () {
      // console.log('数据', this.isEdit, this.currentData)
      if (this.isEdit) {
        this.cateform.name = this.cate_name
        this.cateform.sort = this.cate_sort;
        (this.cateform as CateAdd).id = this.cate_id
      } else {
        this.cateform.name = ''
        this.cateform.sort = 0
      }
    }
  },
  created () {
    // console.log('created', this.dialogVisible)
  }
  // computed: {
  //   aorb: function () {
  //     return this.isEdit
  //   }
  // }
  // created () {
  // this.handlerEdit()
  // }
  // watch: {
  //   currentData (newVal) {
  //     if (this.isEdit) {
  //       console.log('newVal', newVal)

  //       this.cateform.name = newVal.name
  //       this.cateform.sort = newVal.sort
  //     } else {
  //       console.log('newVal1', newVal)
  //       this.cateform.name = ''
  //       this.cateform.sort = 0
  //     }
  //   }
  // }
})
</script>

<style lang="scss" scoped>

.dialog_form{
  width: 70%;
  margin-left: 2rem;
}
</style>
